class RestApi {
    //用户数据示例
    users=[]
    //群数据示例
    groups = [

    ];
    // 订单
    orders = [

    ];
    baseUrl="http://192.168.8.68:8888"


/*-- 登录页面下拉框查询所有的用户--*/
     findUsers() {

        return  new Promise((resolve, reject) => {
            uni.request({
                url: this.baseUrl+'/reporter/users/list',
                method: 'GET',
                success: (res) => {
                    let response= res.data
                    if (response.code == 200) {
                       // this.users = res.data.rows
                        resolve( res.data.rows)
                    }
                },
                fail:(err)=>{
                    reject('err')
                }
            });
        });
    };

    /*-- 通讯录页面获取好友列表--*/
    findFriends(user) {
        return  new Promise((resolve, reject) => {
            uni.request({
                url:  this.baseUrl+`/reporter/friend/${user.id}`,
                method: 'GET',
                success: (res) => {
                    if (res.data.code == 200) {
                        resolve( res.data.data)
                    }
                },
                fail:(err)=>{
                    reject('err')
                }
            });
        });
        // return this.users.filter((v) => v.id !== user.id);
    }

    /*-- 通讯录页面获取群列表--*/
    findGroups(user) {
        return  new Promise((resolve, reject) => {
            uni.request({
                url: this.baseUrl+`/reporter/group/${user.id}`,
                method: 'GET',
                success: (res) => {
                    if (res.data.code == 200) {
                        resolve( res.data.data)
                    }
                },
                fail:(err)=>{
                    reject('err')
                }
            });
        });

        // return this.groups.filter((v) => v.userList.find((id) => id === user.id));
    }

    /*-- 登录--*/
    findUser(username, password) {
		return  new Promise((resolve, reject) => {
		    uni.request({
		        url: this.baseUrl+'/reporter/users/login',
		        method: 'POST',
				data: {'name':username,password},
		        success: (res) => {
		            if (res.data.code == 200) {
		               // this.users = res.data.rows
		                resolve( res.data.data)
		            }
		        },
		        fail:(err)=>{
		            reject('err')
		        }
		    });
		});
        /* return this.users.find((user) => user.name === username && user.password === password); */
    }

    getOrderList() {
        return this.orders;
    }

    findGroupById(groupId) {
        return  new Promise((resolve, reject) => {
            uni.request({
                url: this.baseUrl+`/reporter/group/groupId/${groupId}`,
                method: 'GET',
                success: (res) => {
                    if (res.data.code == 200) {
                        // this.users = res.data.rows
                        resolve( res.data.data)
                    }
                },
                fail:(err)=>{
                    reject('err')
                }
            });
        });
    }

    /*--根据Id查询用户--*/
    findUserById(id) {
        return  new Promise((resolve, reject) => {
            uni.request({
                url: this.baseUrl+`/reporter/users/${id}`,
                method: 'GET',
                success: (res) => {
                    if (res.data.code == 200) {
                        // this.users = res.data.rows
                        resolve( res.data.data)
                    }
                },
                fail:(err)=>{
                    reject('err')
                }
            });
        });
    }

    findGroupMembers (groupId) {
        return new Promise((resolve, reject) => {
            uni.request({
                url: this.baseUrl+`/reporter/users/groupId/${groupId}`,
                method: 'GET',
                success: (res) => {
                    console.info("group~~~")
                    console.info(res)
                    if (res.data.code == 200) {
                        resolve( res.data.data)
                    }
                },
                fail:(err)=>{
                    reject('err')
                }
            });
        });
    }
}

export default new RestApi();
